<?php
namespace Portal\Controller;
use Library\Task\DbTask;
use Library\Wx;
use \Org\Util\Jwt;
use Think\Crypt\Driver\Think;

/**
 * ／代码测试类
 *
 * @author pufan
 */
if (APP_DEBUG) {
    
class TestController
{

    public function test_rand_by_weight()
    {
        header("Content-Type: text/html;charset=utf-8");
        set_time_limit(0);//0表示不限时
        error_reporting(E_ALL);
        ini_set('display_errors', 'On');

        $shake = new \Library\Shake();
        $shake->test_rand_by_weight_group();
    }

    public function test_wx()
    {
        $_SESSION['open_id'] = 'o9Vj9vno48jVaNUSLDiIYhR8elGM';
        $open_id = 'o9Vj9vno48jVaNUSLDiIYhR8elGM';
        $result = Wx::getUserInfo($open_id);
        var_dump($result);

        $mark = 'kaifa';
        $result = Wx::updateUserRemark($open_id, $mark);
        var_dump($result);
    }

    /*
     * 测试全局 原子加和原子减
     */
    public function test_thikphp_cache()
    {
        S('guowei_test', 1, 600);
        redisIncr('guowei_test');
        redisIncr('guowei_test');
        redisIncr('guowei_test');

        redisDecr('guowei_test');
        redisDecr('guowei_test');
        dump(' 2 is ok');
        echo 'result:' . S('guowei_test');
    }

    public function SwitchingBeacon()
    {
        $wxevent = new \Library\WxEvent();
        /*
         * guowei
         */
        $event = '{"ToUserName":"gh_e52449c80414","FromUserName":"o9Vj9vno48jVaNUSLDiIYhR8elGM","CreateTime":"1465813343","MsgType":"event","Event":"ShakearoundUserShake","ChosenBeacon":{"Uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825","Major":"10060","Minor":"15179","Distance":"4.3305555145975712","Rssi":"-68","MeasurePower":"-68"},"ChosenPageId":"3133581"}';
        $wxevent->handle_shakearoundusershake($event);
    }

    /*    public function testMysqlEvent(){
            set_time_limit(0);
            $order = D('prizes_order');
            $curTime = time();
            $curTime= $curTime + 120;

            echo date('y-m-d H:i:s', $curTime);
            echo '<br>';
            for($loopNum = 600; $loopNum > 0; $loopNum--) {
                $curTime++;
                for($i=0; $i < 1000; $i++) {
                    $eventTime = date('y-m-d H:i:s', $curTime);
                    $sql = 'CREATE DEFINER=`bbt`@`%` EVENT `TestEvent'.$curTime.$i.'` ON SCHEDULE AT "'.$eventTime.'" ON COMPLETION NOT PRESERVE ENABLE DO UPDATE   `yy_user_prizes_count` SET `cur_day_num` = 0   WHERE  `update_time`  < curdate()';
                    $result = $order->execute($sql);

                }
            }
            echo $eventTime;
            echo '<br>';
        }*/

    public function testMysqlEvent()
    {
        set_time_limit(0);
        $order = D('prizes_order');
        $curTime = time();
        $curTime = $curTime + 3600;

        $eventTime = date('y-m-d H:i:s', $curTime);

        $taskid = 'test' . $curTime;
        $data = 'UPDATE  `yy_user_prizes_count` SET `cur_day_num` = 0   WHERE  `update_time`  < curdate()';
        $sche = new DbTask();
        $sche->add($taskid, $data, $eventTime);


        /*
        $curTime = $curTime - 1800;
         $eventTime = date('y-m-d H:i:s', $curTime);
         $sche->updateTime($taskid, $eventTime);

         echo $eventTime;
         $sche->del($taskid);*/
        echo '<br>';
    }

    public function testRdsMysqlEvent()
    {
        try {
            $order = D();
            $curTime = time();
            $curTime = $curTime + 60 * 10;
            $eventTime = date('y-m-d H:i:s', $curTime);
            $sql = 'CREATE DEFINER=`' . C('DB_USER') . '`@`%` EVENT `bbtTask_recoveryCardCode_496_7' . mt_rand(10000000000, 99999999999) . '` ON SCHEDULE AT "' . $eventTime . '"
ON COMPLETION NOT PRESERVE ENABLE DO ' . 'select now()';
            $result = $order->execute($sql);
            echo $order->getLastSql();
            exit;
        } catch (\Exception $e) {
            $result = FALSE;
            \Think\Log::record('add Task catch Exception' . $e->getMessage());
            return false;
        }
        return true;
    }
    public function testJwt() {
        $payload['iat'] = time();
        $payload['exp'] = time()+3600*24*365;
        $payload['exp'] = time()+3600*24*365;

        $jwt = new Jwt();
        $token = $jwt->encode($payload, 'ZF1RwrsckIA37hQx', $algo = 'HS256');
        echo $token;
        echo '<br>';
        try {
            $payload_out = $jwt->decode($token, '');
        } catch (\Exception $e) {
            echo ('verification catch Exception'.$e->getMessage());
        }
        var_dump($payload_out);
        exit;
    }
}
}

